'''
---------------------------------------------------------------------------------------------------------
Author: LinYushu - zwt - xdxgkxq
Email: linyushu0513@qq.com

File Created:	2023/03/Sa Saturday 19:43:55
Last Modified:	2023/03/Sa Saturday 21:27:01

Copyright 2023@LinYushu - zwt - xdxgkxq
---------------------------------------------------------------------------------------------------------
'''

import os
import json
from time import sleep
from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options


def getInfo():
    global name
    global password
    try:
        with open("info.json")as f:
            data = json.load(f)
            name = data[0]
            password = data[1]
    except:
        name = input("Please input your account: ")
        password = input("Please input your password: ")
        data = [name, password]
        with open("info.json", "a+")as f:
            json.dump(data, f)


def LoginVPN():
    options = Options()
    options.add_experimental_option(
        "excludeSwitches", ['enable-automation', 'enable-logging'])
    driver = wd.Edge(options=options)

    url = 'https://vpn.shiep.edu.cn/por/login_psw.csp?rnd=0.'
    driver.get(url)
    sleep(1.5)
    try:
        driver.find_element(By.ID, "svpn_name").send_keys(name)
        sleep(0.1)
        driver.find_element(By.ID, "svpn_password").send_keys(password)
        sleep(0.1)
        driver.find_element(By.ID, "logButton").click()
        sleep(1)
        
        while 1:
            driver.switch_to.frame('banners')
            #不得不说这是一句非常弱智的代码，通过把真·CPU给干烧了，达到等待到登录成功的目的
            # try:
            #     target = driver.find_element(By.XPATH,'//*[@id="main"]/div[1]/ul/li/p[1]/span[4]/a')
            #     if not target:
            #         break
            # except:
            #     print('No found!')
            #     sleep(0.2)
    except:
        sleep(3)


if __name__ == "__main__":
    getInfo()
    LoginVPN()
